import { HOST } from '../core/dict';
import logger from '../core/logger';
import { clientBase } from './clientBase';
import { appType } from '../models/enums/appType';
import { typeInfo } from '../models/apis/typeInfo';
import ajaxResult from '../models/apis/ajaxResult';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class typeClient extends clientBase {
  constructor() {
    super();
  }

  public getAppTypes(): Promise<ajaxResult<Array<typeInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/type/getAll?type=${appType.app}`)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: Array<typeInfo> = response.data.result;
            resolve(this.buildSuccessResult<Array<typeInfo>>(ret));
          } else
            resolve(this.buildFailedResult<Array<typeInfo>>(response.statusText));
        }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.buildExceptionResult<Array<typeInfo>>(err));
        });
    });
  }

  public getGameTypes(): Promise<ajaxResult<Array<typeInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/type/getAll?type=${appType.game}`)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: Array<typeInfo> = response.data.result;
            resolve(this.buildSuccessResult<Array<typeInfo>>(ret));
          } else
            resolve(this.buildFailedResult<Array<typeInfo>>(response.statusText));
        })
        .catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.buildExceptionResult<Array<typeInfo>>(err));
        });
    });
  }
}
export default new typeClient();